package mindmap.operation;

import mindmap.components.node.node;
import mindmap.controller.layoutController;

import java.util.Collections;

import static mindmap.controller.layoutController.selectRect;
/**
 * 左布局
 */
public class LeftLayout {

    private  node root;

    public node getRoot() {
        return root;
    }

    public void setRoot(node root) {
        this.root = root;
    }

    public LeftLayout(node root) {
        this.root = root;

    }

    public void change(){
        double x = root.getLayoutX();
        if(root.arrangement.equals("left")){
            return;
        }
        if(root.arrangement.equals("right")){
            root.arrangement = "left";
            node.deleteList.clear();
            root.sumDeleteNode(root);
            for(node n : node.deleteList){
                n.arrangement = "left";
                n.getLine().startXProperty().unbind();
                n.getLine().startXProperty().bind(n.getParentNode().layoutXProperty());
                n.getLine().endXProperty().unbind();
                n.getLine().endXProperty().bind(n.layoutXProperty().add(n.getRectangle().getWidth()));
                n.setLayoutX(2*x - n.getLayoutX());
            }
        } else {
            if(node.leftMinHeight!=null&&node.rightMaxHeight!=null) {
                //表示要下降的高度
                double h = node.leftMinHeight.getLayoutY() - node.rightMaxHeight.getLayoutY() + 50;
                root.arrangement = "left";
                Collections.sort(node.leftChildrenNode, (o1, o2) -> {
                    return (int) o1.getLayoutY() - (int) o2.getLayoutY();
                });
                Collections.sort(node.rightChildrenNode, (o1, o2) -> {
                    return (int) o1.getLayoutY() - (int) o2.getLayoutY();
                });
                node.leftChildrenNode.get(node.leftChildrenNode.size() - 1).SufNode = node.rightChildrenNode.get(0);
                node.rightChildrenNode.get(0).PreNode = node.leftChildrenNode.get(node.leftChildrenNode.size() - 1);
                node.deleteList.clear();
                root.sumDeleteNode(root);
                for (node n : node.deleteList) {
                    if (n.arrangement.equals("right")) {
                        n.arrangement = "left";
                        n.getLine().startXProperty().unbind();
                        n.getLine().startXProperty().bind(n.getParentNode().layoutXProperty());
                        n.getLine().endXProperty().unbind();
                        n.getLine().endXProperty().bind(n.layoutXProperty().add(n.getRectangle().getWidth()));
                        n.setLayoutX(2 * x - n.getLayoutX());
                        n.setLayoutY(n.getLayoutY() + h);
                    }
                }
                node.leftMaxHeight = null;
                node.leftMinHeight = null;
                node.rightMaxHeight = null;
                node.rightMinHeight = null;
                node.leftChildrenNode.clear();
                node.rightChildrenNode.clear();
                root.ConfirmBoth(root);
                root.setLayoutY((root.maxHeightNode.getLayoutY() + root.minHeightNode.getLayoutY()) / 2);
            }
            root.arrangement = "left";
        }
        layoutController.getInstance().hideSelectRect();
        layoutController.getInstance().selectNode = null;

    }

}
